package com.clkj.goldpartner.controller;

import com.clkj.goldpartner.pojo.Goods;
import com.clkj.goldpartner.pojo.common.DateFormatHelper;
import com.clkj.goldpartner.pojo.common.Result;
import com.clkj.goldpartner.pojo.common.StatusCode;
import com.clkj.goldpartner.service.GoodsService;
import io.swagger.annotations.*;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

/**
 * @author 杨阿旺
 * @date 2021-03-02 13:57
 */
@RestController
@RequestMapping(value = "/goods")
@CrossOrigin
@Api(value = "/api", tags = {"商品管理"})
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    /**
     * 添加商品
     *
     * @param goods
     * @return
     */
    @PostMapping("/addGoods")
    @ApiOperation(value = "添加商品")
    public Result addGoods(Goods goods) {
        try {
            String goods_num = goodsService.getGoods_num();
            Integer id = 0;
            if (goods_num != null) {
                id = Integer.parseInt(goods_num) + 1;
            } else {
                id = 1;
            }
            goods_num = String.format("%08d", id);
            goods.setGoods_num(goods_num);
            goods.setGoods_status("0");
            goods.setCreated_time(DateFormatHelper.currentTimes());
            Integer goods_id = goodsService.addGoods(goods);
            return new Result(true, StatusCode.OK, "添加成功", goods_id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "添加失败", 0);
        }
    }

    /**
     * 获取商品列表
     *
     * @param names
     * @return
     */
    @PostMapping("/listGoods")
    @ApiOperation(value = "模糊查询，获取商品列表")
    public Result listGoods(String names, Integer shop_id,String UDI) {
        try {
            List<Map> goodsList = goodsService.listGoods(names, shop_id,UDI);
            return new Result(true, StatusCode.OK, "查询成功", goodsList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "查询失败", null);
        }
    }

    /**
     * 商品页列表
     *
     * @param goods_num
     * @param goods_name
     * @param goods_format
     * @param category_id
     * @return
     */
    @PostMapping("/listGoodsSearch")
    @ApiOperation(value = "商品页列表")
    public Result listGoodsSearch(Integer shop_id, String goods_num, String goods_name, String goods_format, Integer category_id,String UDI) {
        try {
            List<Map> goodsList = goodsService.listGoodsSearch(shop_id, goods_num, goods_name, goods_format, category_id,UDI);
            return new Result(true, StatusCode.OK, "查询成功", goodsList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "查询失败", null);
        }
    }

    /**
     * 删除商品
     *
     * @param ids
     * @return
     */
    @PostMapping("/updateStatus")
    @ApiOperation(value = "删除商品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "输入需要删除的商品id字符串", required = true, dataType = "String", paramType = "query")})
    public Result updateStatus(String ids) {
        try {
            goodsService.updateStatus(ids);
            return new Result(true, StatusCode.OK, "操作成功", true);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "操作失败", false);
        }
    }

    @PostMapping("/listGoodsTotal")
    @ApiOperation(value = "商品进货汇总")
    public Result listGoodsTotal(String goods_num, Integer shop_id) {
        try {
            List<Map<String, Object>> goodsListTotal = goodsService.goodsListTotal(goods_num, shop_id);
            return new Result(true, StatusCode.OK, "查询成功", goodsListTotal);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "操作失败", "error");
        }
    }

    @GetMapping("/exportGoodsTotal")
    @ApiOperation(value = "商品进货汇总表导出")
    public void exportGYSDetail(String goods_num, Integer shop_id, HttpServletResponse response) throws IOException {
        try {
            List<Map<String, Object>> goodsListTotal = goodsService.goodsListTotal(goods_num, shop_id);
            HSSFWorkbook wb = new HSSFWorkbook();
// 根据页面index 获取sheet页
            HSSFSheet sheet = wb.createSheet("商品进货汇总表");
            int i = 0;
            HSSFRow row = sheet.createRow(0);
            row.createCell(0).setCellValue("序号");
            row.createCell(1).setCellValue("商品名称");
            row.createCell(2).setCellValue("总数量");
            row.createCell(3).setCellValue("UDI");
            for (Map root : goodsListTotal) {
                row = sheet.createRow(i + 1);
                row.createCell(0).setCellValue(i + 1);
                row.createCell(1).setCellValue(root.get("goods_name").toString());
                row.createCell(2).setCellValue(root.get("goods_number").toString());
                row.createCell(3).setCellValue(root.get("UDI").toString());
                i++;
            }
// 输出Excel文件
            OutputStream output = response.getOutputStream();
            response.reset();
// 设置文件头
            response.setHeader("Content-Disposition",
                    "attchement;filename=" + new String("商品进货汇总表.xls".getBytes("gb2312"), "ISO8859-1"));
            response.setContentType("application/msexcel");
            wb.write(output);
            wb.close();
        } catch (NumberFormatException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @PostMapping("/listGyGoodsTotal")
    @ApiOperation(value = "供货商供货汇总")
    public Result listGyGoodsTotal(Integer shop_id, Integer supplier_id, String start_time, String end_time) {
        try {
            List<Map<String, Object>> goodsListTotal = goodsService.goodsListGyTotal(shop_id, supplier_id, start_time, end_time);
            return new Result(true, StatusCode.OK, "查询成功", goodsListTotal);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "操作失败", "error");
        }
    }

    @GetMapping("/exportlistGyGoodsTotal")
    @ApiOperation(value = "供应商供货汇总")
    public void exportlistGyGoodsTotal(String supplier_names, String goods_number, String goods_total, HttpServletResponse response) throws IOException {
        try {
            String names[] = supplier_names.split(",");
            String numbers[] = goods_number.split(",");
            String totals[] = goods_total.split(",");
            HSSFWorkbook wb = new HSSFWorkbook();
// 根据页面index 获取sheet页
            HSSFSheet sheet = wb.createSheet("供应商供货汇总表");
            int i = 0;
            HSSFRow row = sheet.createRow(0);
            row.createCell(0).setCellValue("序号");
            row.createCell(1).setCellValue("供应商名称");
            row.createCell(2).setCellValue("供货品种数");
            row.createCell(3).setCellValue("进货总数");
            for (int j = 0; j < names.length; j++) {
                row = sheet.createRow(i + 1);
                row.createCell(0).setCellValue(i + 1);
                row.createCell(1).setCellValue(names[j]);
                row.createCell(2).setCellValue(totals[j]);
                row.createCell(3).setCellValue(numbers[j]);
                i++;
            }
// 输出Excel文件
            OutputStream output = response.getOutputStream();
            response.reset();
// 设置文件头
            response.setHeader("Content-Disposition",
                    "attchement;filename=" + new String("供应商供货汇总表.xls".getBytes("gb2312"), "ISO8859-1"));
            response.setContentType("application/msexcel");
            wb.write(output);
            wb.close();
        } catch (NumberFormatException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @PostMapping("/listGoodsSaleTotal")
    @ApiOperation(value = "货品销售汇总")
    public Result listGoodsSaleTotal(Integer shop_id, String customer_name, String start_time, String end_time) {
        try {
            List<Map<String, Object>> goodsListTotal = goodsService.listGoodsSaleTotal(shop_id, customer_name, start_time, end_time);
            return new Result(true, StatusCode.OK, "查询成功", goodsListTotal);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "操作失败", "error");
        }
    }

    @GetMapping("/exportlistGoodsSaleTotal")
    @ApiOperation(value = "货品销售汇总")
    public void exportlistGoodsSaleTotal(Integer shop_id, String customer_name, String start_time, String end_time, HttpServletResponse response) throws IOException {
        try {
            List<Map<String, Object>> goodsListTotal = goodsService.listGoodsSaleTotal(shop_id, customer_name, start_time, end_time);
            HSSFWorkbook wb = new HSSFWorkbook();
// 根据页面index 获取sheet页
            HSSFSheet sheet = wb.createSheet("货品销售汇总表");
            int i = 0;
            HSSFRow row = sheet.createRow(0);
            row.createCell(0).setCellValue("序号");
            row.createCell(1).setCellValue("商品名称");
            row.createCell(2).setCellValue("批号");
            row.createCell(3).setCellValue("生产日期");
            row.createCell(4).setCellValue("有效期");
            row.createCell(5).setCellValue("销售数量");
            row.createCell(6).setCellValue("UDI");
            for (Map root : goodsListTotal) {
                row = sheet.createRow(i + 1);
                row.createCell(0).setCellValue(i + 1);
                row.createCell(1).setCellValue(root.get("goods_name").toString());
                row.createCell(2).setCellValue(root.get("batch_number").toString());
                row.createCell(3).setCellValue(root.get("production_date").toString());
                row.createCell(4).setCellValue(root.get("expiration").toString());
                row.createCell(5).setCellValue(root.get("goods_number").toString());
                row.createCell(6).setCellValue(root.get("UDI").toString());
                i++;
            }
// 输出Excel文件
            OutputStream output = response.getOutputStream();
            response.reset();
// 设置文件头
            response.setHeader("Content-Disposition",
                    "attchement;filename=" + new String("货品销售汇总表.xls".getBytes("gb2312"), "ISO8859-1"));
            response.setContentType("application/msexcel");
            wb.write(output);
            wb.close();
        } catch (NumberFormatException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @GetMapping("/exportGoodsList")
    @ApiOperation(value = "货品销售汇总")
    public void exportGoodsList(Integer shop_id, String goods_num, String goods_name, String goods_format, Integer category_id,String UDI, HttpServletResponse response) throws IOException {
        try {
            List<Map> goodsList = goodsService.listGoodsSearch(shop_id, goods_num, goods_name, goods_format, category_id,UDI);
            HSSFWorkbook wb = new HSSFWorkbook();
// 根据页面index 获取sheet页
            HSSFSheet sheet = wb.createSheet("货品销售汇总表");
            int i = 0;
            HSSFRow row = sheet.createRow(0);
            row.createCell(0).setCellValue("货品代码");
            row.createCell(1).setCellValue("货品名称");
            row.createCell(2).setCellValue("型号及规格");
            row.createCell(3).setCellValue("单位");
            row.createCell(4).setCellValue("生产企业");
            row.createCell(5).setCellValue("注册证号/备案凭证号");
            row.createCell(6).setCellValue("生产许可证号");
            row.createCell(7).setCellValue("储存条件");
            row.createCell(8).setCellValue("养护方式");
            row.createCell(9).setCellValue("养护周期");
            row.createCell(10).setCellValue("类别");
            row.createCell(11).setCellValue("备注");
            row.createCell(12).setCellValue("UDI");
            for (Map root : goodsList) {
                row = sheet.createRow(i + 1);
                row.createCell(0).setCellValue(root.get("goods_num").toString());
                row.createCell(1).setCellValue(root.get("goods_name").toString());
                row.createCell(2).setCellValue(root.get("goods_format").toString());
                row.createCell(3).setCellValue(root.get("goods_unit").toString());
                row.createCell(4).setCellValue(root.get("product_factory").toString());
                row.createCell(5).setCellValue(root.get("registration_NO").toString());
                row.createCell(6).setCellValue(root.get("production_NO").toString());
                row.createCell(7).setCellValue(root.get("storage_mode").toString());
                row.createCell(8).setCellValue(root.get("curing_type").toString());
                row.createCell(9).setCellValue(root.get("curing_time").toString());
                row.createCell(10).setCellValue(root.get("category_name").toString());
                row.createCell(11).setCellValue(root.get("goods_note").toString());
                row.createCell(12).setCellValue(root.get("UDI").toString());
                i++;
            }
// 输出Excel文件
            OutputStream output = response.getOutputStream();
            response.reset();
// 设置文件头
            response.setHeader("Content-Disposition",
                    "attchement;filename=" + new String("货品管理表.xls".getBytes("gb2312"), "ISO8859-1"));
            response.setContentType("application/msexcel");
            wb.write(output);
            wb.close();
        } catch (NumberFormatException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
